use "$datapath/A3_gen_empmark.dta",clear

*Dropping dates prior to  and after 2014m12 (since we want to follow people for at least 2 years)
drop if notdate_def<=ym(2004,12) | notdate_def>ym(2016,12)

*Dropping small notifications
drop if antvars<5

*Dropping negative notification times
keep if inkom_dat<anstupp_dat


bys lopnr : egen Xsumwork = total(working2) if inrange(eventtime_def,-12,-1)
bys lopnr : egen sumwork = max(Xsumwork)

gen eventtime_ankomst = date - ym(year(ankomst_dat) , month(ankomst_dat))
keep if inrange(eventtime_def,-12,36)

merge m:1 persid date using "$datapath/AKUsearch.dta"
drop  if _merge==2
drop _merge


{ // Generate De jure notification time (DJNT)
*===============================================================================

gen 	de_jure_NT_age = .
replace de_jure_NT_age = 30 if inrange(tenureatnot,0,23) & startdate3>=ym(1997,1)
replace de_jure_NT_age = 60 if inrange(tenureatnot,24,47) & startdate3>=ym(1997,1)
replace de_jure_NT_age = 90 if inrange(tenureatnot,48,71) & startdate3>=ym(1997,1)
replace de_jure_NT_age = 120 if inrange(tenureatnot,72,95) & startdate3>=ym(1997,1)
replace de_jure_NT_age = 150 if inrange(tenureatnot,96,119) & startdate3>=ym(1997,1)
replace de_jure_NT_age = 180 if tenureatnot>=120 & tenureatnot!=. & startdate3>=ym(1997,1)
replace de_jure_NT_age = 365 if age_def>=55 & tenureatnot>=10*12 & tenureatnot!=. & WC==1

replace de_jure_NT_age = 30 if inrange(age_def,0,24.99) & startdate3<ym(1997,1)
replace de_jure_NT_age = 60 if inrange(age_def,25,29.99) & startdate3<ym(1997,1)
replace de_jure_NT_age = 90 if inrange(age_def,30,34.99) & startdate3<ym(1997,1)
replace de_jure_NT_age = 120 if inrange(age_def,35,39.99) & startdate3<ym(1997,1)
replace de_jure_NT_age = 150 if inrange(age_def,40,44.99) & startdate3<ym(1997,1)
replace de_jure_NT_age = 180 if age_def>=45 & startdate3<ym(1997,1) & age_def!=.

replace de_jure_NT_age = round(de_jure_NT_age/30)
count if de_jure_NT_age==. 	
count if tenureatnot==. 	

}
*

{ // Generate De jure notification time (DJNT)
*===============================================================================

gen 	de_jure_NT_age2 = .
replace de_jure_NT_age2 = 1 if inrange(tenureatnot,0,23) 
replace de_jure_NT_age2 = 2 if inrange(tenureatnot,24,47)
replace de_jure_NT_age2 = 3 if inrange(tenureatnot,48,71)
replace de_jure_NT_age2 = 4 if inrange(tenureatnot,72,95)
replace de_jure_NT_age2 = 5 if inrange(tenureatnot,96,119) 
replace de_jure_NT_age2 = 6 if tenureatnot>=120 & tenureatnot!=.
replace de_jure_NT_age2 = 12 if age_def>=55 & tenureatnot>=120 & tenureatnot!=. & WC==1


count if de_jure_NT_age2==. 	
count if tenureatnot==. 	

}
*

{ // Normalize control variables 
*===============================================================================
gen age_sq = age_def^2
foreach var of   varlist annual_ear_prenot_def female immigrant tenureatnot educ1 educ2 educ3 age_def age_sq  {
	qui sum `var' 	
	gen C_`var' = `var' - r(mean)
}	

}
*
*Generate variables
sum de_jure_NT_age if eventtime_def==0,d
gen dejure3 = de_jure_NT_age>3
gen above55 = age_def>55
*Set global
global covariets 	"C_annual_ear_prenot_def C_female C_immigrant C_age_def  C_tenureatnot C_educ1 C_educ2 C_educ3 "	

{ // Merge on PES meetings data
*===============================================================================
merge m:1 persid date using "$datapath/hist_v2.dta"
drop if _merge==2
drop _merge


gen contact = 0
forvalues i=1(1)5{
	replace kontakt_typ`i'=0 if kontakt_typ`i'==.
	replace contact = 1 if kontakt_typ`i'==1 & contact==0	
}	

egen Ncontact = rowtotal(kontakt_typ*)
gen PES_contact = Ncontact>0 if Ncontact!=.
}
*



gen 	search_ind = max(search_databa, search_direct, search_postads, search_recruit)
replace search_ind = 0  if search!=. & search_ind!=1
replace search_int = 0 if search!=. & search_int==.
gen 	search_pes = max(search_afcoach, search_afself)
replace search_pes = 0 if search!=. & search_pes==.

gen search_int_arc = ln(search_int + sqrt(search_int^2 +1))
gen Ncontact_arc = ln(Ncontact + sqrt(Ncontact^2 +1))


keep if  inrange(eventtime_def,-12,24)
drop if varselorsak==5

{ // Search graph 
*===============================================================================
preserve
	gen diff = (ankomst_dat-inkom_dat)/30
		sum diff
		local d = `r(mean)'
	gcollapse (mean) search search_int_arc  Ncontact_arc contact  , by(eventtime_def)

	
	twoway  (connected search contact search_int_arc  Ncontact_arc   eventtime_def, lcolor(black maroon black maroon   ) ///
	ms(o s oh sh ) mcolor(black maroon black maroon    ) lpattern(solid solid longdash  longdash  )) ///
	,xline(0 , lpattern(dash) lcolor(black ) lstyle(foreground ))  ///
	xline(`d', lpattern(dash_dot) lcolor(black ) lstyle(foreground )) ///
	xline(3, lpattern(dot) lcolor(black ) lstyle(foreground )) ///
	graphregion(color(white)) ///
	ytitle("Search")  name(c,replace) ///
	xtitle("Month relative to notification") xscale(r(-12 24)) xlab(-12(6)24) ///
	legend(order(1 "Extensive search (LFS)" 2 "Extensive search (PES)"  3 "Search intensity (LFS)" 4  "Search intensity (PES)" ) col(2) size(small) region(color(white)) ) ///
	yscale(r(0 .6)) ylab(0(.1).6)
	
	graph export "$agegraphpath/Search/RAW_search_combined_v3.pdf",replace
	graph export "$agegraphpath/Search/RAW_search_combined_v3.eps",replace as(eps)
restore	
}
*
{ // Search intensity by defacto NT
*===============================================================================
preserve
	sum nottime_def,d
	gen defacto_above = nottime_def>`r(p50)'
	gen diff = (ankomst_dat-inkom_dat)/30
	replace nottime_def=nottime_def/30
		 sum de_jure_NT_age if defacto_above==0 & eventtime_def==0,d
		 sum de_jure_NT_age if defacto_above==1 & eventtime_def==0,d
		 
		 
		sum nottime_def if defacto_above==0 & eventtime_def==0,d
		 sum nottime_def if defacto_above==1 & eventtime_def==0,d 

		bys defacto_above : sum diff
		sum diff
		local d = `r(mean)'
	gcollapse (mean) search search_int_arc    , by(eventtime_def defacto_above)
	

	twoway  (connected search_int_arc  eventtime_def if defacto_above==0, ms(oh) mcolor(black) lcolor(black) lpattern(dash)) ///
		(connected search_int_arc  eventtime_def if defacto_above==1, ms(o) mcolor(black) lcolor(black) lpattern(solid)) ///
	,xline(0 , lpattern(dash) lcolor(black ) lstyle(foreground ))  ///
	xline(`d', lpattern(dash_dot) lcolor(black ) lstyle(foreground )) ///
	graphregion(color(white)) ///
	ytitle("Search inentisty (LFS)")  name(c,replace) ///
	xtitle("Month relative to notification") xscale(r(-12 24)) xlab(-12(6)24) ///
	legend(order(1 "Short notice" 2 "Long notice" ) col(3) size(small) region(color(white)) ) ///
	
	
	graph export "$agegraphpath/Search/RAW_search_bydefacto.pdf",replace
	graph export "$agegraphpath/Search/RAW_search_bydefacto.eps",replace as(eps)
restore	
}
*
{ // Search graph  by defacto
*===============================================================================
forvalues i =0(1)1{
	preserve
		qui sum nottime_def,d
		gen defacto_above = nottime_def>`r(p50)'
		keep if defacto_above==`i'
		gen diff = (ankomst_dat-inkom_dat)/30
		sum diff
		local d = `r(mean)'
		
		gcollapse (mean) search search_int_arc  Ncontact_arc contact  , by(eventtime_def)
		
		
		twoway  (connected search contact search_int_arc  Ncontact_arc   eventtime_def, lcolor(black maroon black maroon   ) ///
		ms(o s oh sh ) mcolor(black maroon black maroon    ) lpattern(solid solid longdash  longdash  )) ///
		,xline(0 , lpattern(dash) lcolor(black ) lstyle(foreground ))  ///
		xline(`d', lpattern(dash_dot) lcolor(black ) lstyle(foreground )) ///
		graphregion(color(white)) ///
		ytitle("Search")  name(d`i',replace) ///
		xtitle("Month relative to notification") xscale(r(-12 24)) xlab(-12(6)24) ///
		legend(order(1 "Extensive search (LFS)" 2 "Extensive search (PES)"  3 "Search intensity (LFS)" 4  "Search intensity (PES)" ) col(2) size(small) region(color(white)) ) ///
		yscale(r(0 .8)) ylab(0(.2).8)
		
		graph export "$agegraphpath/Search/RAW_search_combined_highdefacto`i'.pdf",replace
	restore	
}
}
*

/*
*Extensive Margins
preserve
	gcollapse (mean)  search   PES_contact , by(eventtime_def)	

	
	twoway  (connected    search PES_contact  eventtime_def, lcolor(black black  ) ms(o oh ) mcolor(black black  ) lpattern(solid dash )) ///
	,xline(0 , lpattern(dash) lcolor(black ) lstyle(foreground ))  ///
	xline(-2, lpattern(dash_dot) lcolor(black ) lstyle(foreground )) ///
	xline(3, lpattern(dot) lcolor(black ) lstyle(foreground )) ///
	graphregion(color(white)) ///
	ytitle("Pr(search)")  name(a,replace) ///
	xtitle("Month relative to notification") xscale(r(-12 24)) xlab(-12(3)24) ///
	legend(order(1 "Pr(Search)" 2 "PES registration" ) region(color(white)) ) ///
	yscale(r(0 .5)) ylab(0(.1).5)
	
	graph export "$agegraphpath/Search/RAW_search_extensive.pdf",replace
restore	


*Intensive Margins
preserve
	gcollapse (mean) search_int_arc  Ncontact_arc  , by(eventtime_def)
	
	*Normalize variables
	qui sum search_int_arc if eventtime==-6
	replace search_int_arc = search_int_arc - `r(mean)'
	qui sum Ncontact_arc if eventtime==-6
	replace Ncontact_arc = Ncontact_arc - `r(mean)'
	
	twoway  (connected   search_int_arc Ncontact_arc  eventtime_def, lcolor(black black  ) ms(o oh) mcolor(black black   ) lpattern(solid dash   )) ///
	,xline(0 , lpattern(dash) lcolor(black ) lstyle(foreground ))  ///
	xline(-2, lpattern(dash_dot) lcolor(black ) lstyle(foreground )) ///
	xline(3, lpattern(dot) lcolor(black ) lstyle(foreground )) ///
	graphregion(color(white)) ///
	ytitle("Search intensity")  name(b,replace) ///
	xtitle("Month relative to notification") xscale(r(-12 24)) xlab(-12(3)24) ///
	legend(order(1 "Search intensity" 2 "# of PES interactions") region(color(white)) ) ///
	yscale(r(0 .5)) ylab(0(.1).5)
	
	graph export "$agegraphpath/Search/RAW_search_intensity.pdf",replace
restore	



*Combined
preserve
	gcollapse (mean) search search_int_arc  Ncontact_arc PES_contact , by(eventtime_def)
	
	*Normalize variables
	*qui sum search_int_arc if eventtime==-6
	*replace search_int_arc = search_int_arc - `r(mean)'
	*qui sum Ncontact_arc if eventtime==-6
	*replace Ncontact_arc = Ncontact_arc - `r(mean)'
	
	twoway  (connected search  search_int_arc PES_contact Ncontact_arc   eventtime_def, lcolor(black black maroon maroon  ) ///
	ms(o oh o oh) mcolor(black black maroon maroon   ) lpattern(solid longdash  solid longdash )) ///
	,xline(0 , lpattern(dash) lcolor(black ) lstyle(foreground ))  ///
	xline(-2, lpattern(dash_dot) lcolor(black ) lstyle(foreground )) ///
	xline(3, lpattern(dot) lcolor(black ) lstyle(foreground )) ///
	graphregion(color(white)) ///
	ytitle("Search")  name(c,replace) ///
	xtitle("Month relative to notification") xscale(r(-12 24)) xlab(-12(6)24) ///
	legend(order(1 "Pr(search)" 2 "Search intensity" 3 "Pr(registered at PES)" 4 "# of PES interactions" ) region(color(white)) ) ///
	yscale(r(0 .5)) ylab(0(.1).6)
	
	graph export "$agegraphpath/Search/RAW_search_combined.pdf",replace
restore	
*/

/*



{ // Event study: Search intensity (DUMMY DEJURE)
*===============================================================================
preserve
	xtset ym_def
	replace search_int = 1 if search==1 & search_int==0
	replace search_int = 0 if search==0
	keep if inrange(eventtime_def,-6,12)
	keep if sumwork>=6
	
	replace eventtime_def =  eventtime_def+6
	xtreg search_int i.dejure3##i.eventtime_def  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	
	gen 	nvals = sum(1) in 4/21
	replace nvals = sum(1) in 42/59
	gen cons = .1991279
	keep if nvals!=.
	gen 	g = 1 in 1/18
	replace g = 2 in 19/36
	
	keep B1 SE1 nvals g cons
	keep if nvals!=.
	save "$datapath/B2_search_temp.dta",replace
	
	keep if g==1 
	keep B1 nvals
	rename B1 C
	tempfile temp
	save `temp'
restore

preserve
	use "$datapath/B2_search_temp.dta",clear
	merge m:1 nvals using `temp'
	drop _merge
	drop if g==1
	
	gen 	T = C + B1 + cons
	replace C = C + cons
	gen sig = abs(B1/sqrt(SE1)) >=1.96
	

	gen time = nvals -6
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected T time, `Tstyle') (connected C time, `Cstyle')  (rspike T C time if sig==1, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to notification") ytitle("Search intensity") name(a2,replace) ///
	graphregion(color(white))  legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white))) 

	graph export "$agegraphpath/Search/eventstudy_searchintensity_dummy_inkomdat.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*

{ // Event study: PES search (DUMMY DEJURE)
*===============================================================================
preserve
	xtset ym_def
	keep if inrange(eventtime_def,-6,12)
	keep if sumwork>=6
	
	replace eventtime_def =  eventtime_def+6
	xtreg Ncontact i.dejure3##i.eventtime_def  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	
	gen 	nvals = sum(1) in 4/21
	replace nvals = sum(1) in 42/59
	gen cons = .1991279
	keep if nvals!=.
	gen 	g = 1 in 1/18
	replace g = 2 in 19/36
	
	keep B1 SE1 nvals g cons
	keep if nvals!=.
	save "$datapath/B2_search_tempPES.dta",replace
	
	keep if g==1 
	keep B1 nvals
	rename B1 C
	tempfile temp
	save `temp'
restore

preserve
	use "$datapath/B2_search_tempPES.dta",clear
	merge m:1 nvals using `temp'
	drop _merge
	drop if g==1
	
	gen 	T = C + B1 + cons
	replace C = C + cons
	gen sig = abs(B1/sqrt(SE1)) >=1.96
	

	gen time = nvals -6
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected T time, `Tstyle') (connected C time, `Cstyle')  (rspike T C time if sig==1, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to notification") ytitle("# meetings at PES") name(a1,replace) ///
	graphregion(color(white))  legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white))) 

	graph export "$agegraphpath/Search/eventstudy_searchintensityPES_dummy_inkomdat.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*




keep if inrange(eventtime_ankomst,-12,24)


{ // Event study: Search intensity (DUMMY DEJURE)
*===============================================================================
preserve
	xtset ym_def
	replace search_int = 1 if search==1 & search_int==0
	replace search_int = 0 if search==0
	keep if inrange(eventtime_ankomst,-6,12)
	keep if sumwork>=6
	
	replace eventtime_ankomst =  eventtime_ankomst+6
	xtreg search_int i.dejure3##i.eventtime_ankomst  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	
	gen 	nvals = sum(1) in 4/21
	replace nvals = sum(1) in 42/59
	gen cons = .1991279
	keep if nvals!=.
	gen 	g = 1 in 1/18
	replace g = 2 in 19/36
	
	keep B1 SE1 nvals g cons
	keep if nvals!=.
	save "$datapath/B2_search_temp.dta",replace
	
	keep if g==1 
	keep B1 nvals
	rename B1 C
	tempfile temp
	save `temp'
restore

preserve
	use "$datapath/B2_search_temp.dta",clear
	merge m:1 nvals using `temp'
	drop _merge
	drop if g==1
	
	gen 	T = C + B1 + cons
	replace C = C + cons
	gen sig = abs(B1/sqrt(SE1)) >=1.96
	

	gen time = nvals -6
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected T time, `Tstyle') (connected C time, `Cstyle')  (rspike T C time if sig==1, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to initial layoff report ") ytitle("Search intensity") name(a2,replace) ///
	graphregion(color(white))  legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white))) 

	graph export "$agegraphpath/Search/eventstudy_searchintensity_dummy.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*

{ // Event study: PES search (DUMMY DEJURE)
*===============================================================================
preserve
	xtset ym_def
	keep if inrange(eventtime_ankomst,-6,12)
	keep if sumwork>=6
	
	replace eventtime_ankomst =  eventtime_ankomst+6
	xtreg Ncontact i.dejure3##i.eventtime_ankomst  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	
	gen 	nvals = sum(1) in 4/21
	replace nvals = sum(1) in 42/59
	gen cons = .1991279
	keep if nvals!=.
	gen 	g = 1 in 1/18
	replace g = 2 in 19/36
	
	keep B1 SE1 nvals g cons
	keep if nvals!=.
	save "$datapath/B2_search_tempPES.dta",replace
	
	keep if g==1 
	keep B1 nvals
	rename B1 C
	tempfile temp
	save `temp'
restore

preserve
	use "$datapath/B2_search_tempPES.dta",clear
	merge m:1 nvals using `temp'
	drop _merge
	drop if g==1
	
	gen 	T = C + B1 + cons
	replace C = C + cons
	gen sig = abs(B1/sqrt(SE1)) >=1.96
	

	gen time = nvals -6
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected T time, `Tstyle') (connected C time, `Cstyle')  (rspike T C time if sig==1, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to initial layoff report ") ytitle("# meetings at PES") name(a1,replace) ///
	graphregion(color(white))  legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white))) 

	graph export "$agegraphpath/Search/eventstudy_searchintensityPES_dummy.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*






















{ // DE JURE Notice (relative to PES report)
*===============================================================================
{ // Pr(search) no covars
*===============================================================================
xtset ym_def


matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	dejure3	 	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[dejure3]
	matrix B[`i',4]  = abs(_b[dejure3]/_se[dejure3]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to initial layoff report ") ytitle("Pr(search)") name(a,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_search.pdf",replace

drop t C T sig 

}
*
{ // Pr(search) no covars, cond employment in t=-12 to t-1
*===============================================================================
xtset ym_def

preserve
keep if sumwork==12
matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	dejure3	 	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[dejure3]
	matrix B[`i',4]  = abs(_b[dejure3]/_se[dejure3]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to initial layoff report") ytitle("Pr(search)") name(b,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_search_cond.pdf",replace

drop t C T sig 
restore
}
*
{ // Pr(search) with covars
*===============================================================================
xtset ym_def


matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	dejure3	 $covariets	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[dejure3]
	matrix B[`i',4]  = abs(_b[dejure3]/_se[dejure3]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to initial layoff report") ytitle("Pr(search)") name(e,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_search_covars.pdf",replace

drop t C T sig 

}
*	
{ // Pr(search) with covars, cond employment in t=-12 to t-1
*===============================================================================
xtset ym_def

preserve
keep if sumwork==12
matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	dejure3	 $covariets	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[dejure3]
	matrix B[`i',4]  = abs(_b[dejure3]/_se[dejure3]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to initial layoff report") ytitle("Pr(search)") name(f,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_search_covars_cond.pdf",replace

drop t C T sig 
restore
}
*	
}
*
{ // DE JURE Notice (relative to individual report)
*===============================================================================
{ // Pr(search) no covars
*===============================================================================
xtset ym_def


matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	dejure3	 	if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[dejure3]
	matrix B[`i',4]  = abs(_b[dejure3]/_se[dejure3]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to individual notification ") ytitle("Pr(search)") name(a,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_search_relnot.pdf",replace

drop t C T sig 

}
*
{ // Pr(search) no covars, cond employment in t=-12 to t-1
*===============================================================================
xtset ym_def

preserve
keep if sumwork==12
matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	dejure3	 	if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[dejure3]
	matrix B[`i',4]  = abs(_b[dejure3]/_se[dejure3]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to individual notification") ytitle("Pr(search)") name(a,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_search_cond_relnot.pdf",replace

drop t C T sig 
restore
}
*
{ // Pr(search) with covars
*===============================================================================
xtset ym_def


matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	dejure3	 $covariets	 if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[dejure3]
	matrix B[`i',4]  = abs(_b[dejure3]/_se[dejure3]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to individual notification") ytitle("Pr(search)") name(a,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_search_covars_relnot.pdf",replace

drop t C T sig 

}
*	
{ // Pr(search) with covars, cond employment in t=-12 to t-1
*===============================================================================
xtset ym_def

preserve
keep if sumwork==12
matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	dejure3	 $covariets	 if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[dejure3]
	matrix B[`i',4]  = abs(_b[dejure3]/_se[dejure3]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to individual notification") ytitle("Pr(search)") name(b,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_search_covars_cond_relnot.pdf",replace

drop t C T sig 
restore
}
*	
}
*

{ // Event study: Search intensity (DUMMY DEJURE)
*===============================================================================
preserve
	xtset ym_def
	replace search_int = 1 if search==1 & search_int==0
	replace search_int = 0 if search==0
	keep if inrange(eventtime_ankomst,-6,12)
	keep if sumwork>=5
	
	replace eventtime_ankomst =  eventtime_ankomst+6
	xtreg search_int i.dejure3##i.eventtime_ankomst  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	gen sel = B1 - 1.96*sqrt(SE1)
	gen seu = B1 + 1.96*sqrt(SE1)
	
	keep in 41/59
	gen time = _n -7
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected B1 time, `Tstyle') (rcap sel seu time, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to initial layoff report ") ytitle("Search intensity") name(a2,replace) ///
	graphregion(color(white)) legend(off)  yline(0, `Sstyle')  yscale(r(-.5 .5)) ylab(-.5(.25).5)

	graph export "$agegraphpath/Search/eventstudy_searchintensity_dummy.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*
{ // Event study: Pr(Search) (DUMMY DEJURE)
*===============================================================================
preserve
	xtset ym_def
	replace search_int = 1 if search==1 & search_int==0
	replace search_int = 0 if search==0
	keep if inrange(eventtime_ankomst,-6,12)
	keep if sumwork>=5
	
	replace eventtime_ankomst =  eventtime_ankomst+6
	xtreg search i.dejure3##i.eventtime_ankomst  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	gen sel = B1 - 1.96*sqrt(SE1)
	gen seu = B1 + 1.96*sqrt(SE1)
	
	keep in 41/59
	gen time = _n -7
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected B1 time, `Tstyle') (rcap sel seu time, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to initial layoff report ") ytitle("Pr(search)") name(a2,replace) ///
	graphregion(color(white)) legend(off)  yline(0, `Sstyle')  yscale(r(-.15 .15)) ylab(-.15(.075).15)

	graph export "$agegraphpath/Search/eventstudy_search_dummy.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*
{ // Event study: Search intensity (CONTINIOUS DEJURE)
*===============================================================================
preserve
	xtset ym_def
	replace search_int = 1 if search==1 & search_int==0
	replace search_int = 0 if search==0
	keep if inrange(eventtime_ankomst,-6,12)
	keep if sumwork>=5
	 
	replace eventtime_ankomst =  eventtime_ankomst+6
	xtreg search_int c.de_jure_NT_age##i.eventtime_ankomst  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	gen sel = B1 - 1.96*sqrt(SE1)
	gen seu = B1 + 1.96*sqrt(SE1)
	
	keep in 21/39
	gen time = _n -7
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected B1 time, `Tstyle') (rcap sel seu time, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to initial layoff report ") ytitle("Search intensity") name(a1,replace) ///
	graphregion(color(white)) legend(off)  yline(0, `Sstyle')  yscale(r(-.15 .15)) ylab(-.15(.075).15)

	graph export "$agegraphpath/Search/eventstudy_searchintensity_continious.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*
{ // Event study: Pr(Search) (CONTINIOUS DEJURE)
*===============================================================================
preserve
	xtset ym_def
	replace search_int = 1 if search==1 & search_int==0
	replace search_int = 0 if search==0
	keep if inrange(eventtime_ankomst,-6,12)
	keep if sumwork>=5
	
	replace eventtime_ankomst =  eventtime_ankomst+6
	xtreg search c.de_jure_NT_age##i.eventtime_ankomst  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	gen sel = B1 - 1.96*sqrt(SE1)
	gen seu = B1 + 1.96*sqrt(SE1)
	
	keep in 21/39
	gen time = _n -7
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected B1 time, `Tstyle') (rcap sel seu time, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to initial layoff report ") ytitle("Pr(search)") name(a2,replace) ///
	graphregion(color(white)) legend(off)  yline(0, `Sstyle') 

	graph export "$agegraphpath/Search/eventstudy_search_continious.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*
{ // Event study: Search intensity (CONTINIOUS DEJURE) dejure2
*===============================================================================
preserve
	xtset ym_def
	replace search_int = 1 if search==1 & search_int==0
	replace search_int = 0 if search==0
	keep if inrange(eventtime_ankomst,-6,12)
	keep if sumwork>=5
	 
	replace eventtime_ankomst =  eventtime_ankomst+6
	xtreg search_int c.de_jure_NT_age2##i.eventtime_ankomst  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	gen sel = B1 - 1.96*sqrt(SE1)
	gen seu = B1 + 1.96*sqrt(SE1)
	
	keep in 21/39
	gen time = _n -7
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected B1 time, `Tstyle') (rcap sel seu time, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to initial layoff report ") ytitle("Search intensity") name(a1,replace) ///
	graphregion(color(white)) legend(off)  yline(0, `Sstyle')  yscale(r(-.15 .15)) ylab(-.15(.075).15)

	graph export "$agegraphpath/Search/eventstudy_searchintensity_continious_dejure2.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*
{ // Event study: Pr(Search) (CONTINIOUS DEJURE) dejure2
*===============================================================================
preserve
	xtset ym_def
	replace search_int = 1 if search==1 & search_int==0
	replace search_int = 0 if search==0
	keep if inrange(eventtime_ankomst,-6,12)
	keep if sumwork>=5
	
	replace eventtime_ankomst =  eventtime_ankomst+6
	xtreg search c.de_jure_NT_age2##i.eventtime_ankomst  $covariets, cluster(varselid) fe nonest 
	
	matrix B = e(b)'
	matrix SE = vecdiag(e(V))'
	svmat B 
	svmat SE
	gen sel = B1 - 1.96*sqrt(SE1)
	gen seu = B1 + 1.96*sqrt(SE1)
	
	keep in 21/39
	gen time = _n -7
	
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 
	twoway (connected B1 time, `Tstyle') (rcap sel seu time, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
	xtitle("Months relative to initial layoff report ") ytitle("Pr(search)") name(a2,replace) ///
	graphregion(color(white)) legend(off)  yline(0, `Sstyle') 

	graph export "$agegraphpath/Search/eventstudy_search_continious_dejure2.pdf",replace
restore	
*graph combine a1 a2, graphregion(color(white)) ycommon xcommon col(1)	

}
*



{ // Pr(search intensive) Continous DE JURE with covars, cond employment in t=-12 to t-1
*===============================================================================
preserve
replace search_int = 1 if search==1 & search_int==0
replace search_int = 0 if search==0
*keep if search==1
xtset ym_def



matrix B = J(61,4,.)
local i = 0

forvalues t = -6(1)12{
	local i = `i' + 1
	 xtreg search_int	de_jure_NT_age	 $covariets	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[de_jure_NT_age]
	matrix B[`i',3]  = _b[de_jure_NT_age] + invttail(e(df_r),0.025)*_se[de_jure_NT_age]
	matrix B[`i',4]  = _b[de_jure_NT_age] - invttail(e(df_r),0.025)*_se[de_jure_NT_age] 
}

svmat B
rename B1 t
rename B2 T
rename B3 seu
rename B4 sel

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 


*Graph
twoway (connected T t, `Tstyle')  (rcap sel seu t , `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-6 12)) xlabel(-6(2)12)  ///
xtitle("Months relative to initial layoff report ") ytitle("Pr(search intensive)") name(f,replace) ///
graphregion(color(white)) legend(off)  yline(0, `Sstyle') subtitle("cont. dejure")
graph export "$agegraphpath/Search/event_search_intext_covars_cond_condejure_relnot.pdf",replace

drop t T seu sel  
restore
}
*

{ // Pr(search) Continous DE JURE with covars, cond employment in t=-12 to t-1
*===============================================================================
xtset ym_def

preserve
keep if sumwork>=12
matrix B = J(61,4,.)
local i = 0

forvalues t = -8(1)18{
	local i = `i' + 1
	qui xtreg search	de_jure_NT_age	 $covariets	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[de_jure_NT_age]
	matrix B[`i',3]  = _b[de_jure_NT_age] + invttail(e(df_r),0.025)*_se[de_jure_NT_age]
	matrix B[`i',4]  = _b[de_jure_NT_age] - invttail(e(df_r),0.025)*_se[de_jure_NT_age] 
}

svmat B
rename B1 t
rename B2 T
rename B3 seu
rename B4 sel

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 


*Graph
twoway (connected T t, `Tstyle')  (rcap sel seu t , `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-8 18)) xlabel(-8(4)18)  ///
xtitle("Months relative to initial layoff report") ytitle("Pr(search)") name(f,replace) ///
graphregion(color(white)) legend(off)  yline(0, `Sstyle')
graph export "$agegraphpath/Search/event_search_covars_cond_condejure.pdf",replace

drop t T seu sel  
restore
}
*



{ // Pr(search type) with covars, cond employment in t=-12 to t-1
*===============================================================================
xtset ym_def

preserve
*keep if sumwork>=12
keep if search==1


foreach var of varlist search_afcoach search_afself search_databa search_recruit search_direct search_postads search_readads search_friends search_interview {
	matrix B = J(61,4,.)
local i = 0
forvalues t = 0(1)12{
	local i = `i' + 1
	qui xtreg `var'	dejure3	 $covariets	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[dejure3]
	matrix B[`i',3]  = _b[dejure3] + invttail(e(df_r),0.025)*_se[dejure3]
	matrix B[`i',4]  = _b[dejure3] - invttail(e(df_r),0.025)*_se[dejure3] 
}

svmat B
rename B1 t
rename B2 T
rename B3 seu
rename B4 sel

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(dash) lcolor(black) lstyle(foreground) " 


*Graph
twoway (connected T t, `Tstyle')  (rcap sel seu t , `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 12)) xlabel(0(3)12)  ///
xtitle("Months relative to initial layoff report") ytitle("Pr(search)") name(`var',replace) ///
graphregion(color(white)) legend(off)  yline(0, `Sstyle') subtitle("`var'")
*graph export "$agegraphpath/Search/event_search_covars_cond.pdf",replace

drop t T seu sel  
}
restore
window manage close graph _all
graph combine 	search_afcoach search_afself search_databa  ///
		search_recruit search_direct search_postads ///
		search_readads search_friends search_interview ///
	, graphregion(color(white))	ycommon xcommon subtitle("De jure >3 (dummy)")
graph export "$agegraphpath/Search/event_searchtype_covars_cond_dumdejure.pdf",replace
	
}
*




{ // DE FACTO Notice
*===============================================================================

sum nottime_def,d
preserve
keep if nottime_def>=60
sum nottime_def,d
gen highnot =  nottime_def>=r(p50)

{ // Pr(search) no covars
*===============================================================================
xtset ym_def


matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	highnot	 	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[highnot]
	matrix B[`i',4]  = abs(_b[highnot]/_se[highnot]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to initial layoff report ") ytitle("Pr(search)") name(a,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_defacto_search.pdf",replace

drop t C T sig 

}
*

{ // Pr(search) with covars
*===============================================================================
xtset ym_def


matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	highnot	 $covariets	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[highnot]
	matrix B[`i',4]  = abs(_b[highnot]/_se[highnot]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to initial layoff report") ytitle("Pr(search)") name(a,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_defacto_search_covars.pdf",replace

drop t C T sig 

}
*
{ // Pr(search) no covars, cond employment in t=-12 to t-1
*===============================================================================
xtset ym_def


keep if sumwork==12
matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	highnot	 	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[highnot]
	matrix B[`i',4]  = abs(_b[highnot]/_se[highnot]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to initial layoff report") ytitle("Pr(search)") name(a,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_defacto_search_cond.pdf",replace

drop t C T sig 

}
*	
{ // Pr(search) with covars, cond employment in t=-12 to t-1
*===============================================================================
xtset ym_def


keep if sumwork==12
matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)24{
	local i = `i' + 1
	qui xtreg search	highnot	 $covariets	if eventtime_ankomst==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[highnot]
	matrix B[`i',4]  = abs(_b[highnot]/_se[highnot]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 24)) xlabel(-12(6)24)  ///
xtitle("Months relative to initial layoff report") ytitle("Pr(search)") name(a,replace) ///
graphregion(color(white)) legend(order( 1 "Long notice" 2 "Short notice" ) region(color(white)))
graph export "$agegraphpath/Search/event_defacto_search_covars_cond.pdf",replace

drop t C T sig 

}
*	
restore
}
*


